#!/bin/bash

# shellcheck disable=SC2120

# Provide source directive to shellcheck.
# shellcheck source=meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/power-cmd
source /usr/libexec/phosphor-state-manager/power-cmd
# shellcheck source=meta-facebook/meta-yosemite4/recipes-yosemite4/plat-tool/files/yosemite4-common-functions
source /usr/libexec/yosemite4-common-functions

#IO 0:7 input port for showing slot 1:8 power status
#IO 8:16 output port for controlling slot 1:8 power status
CHASSIS_ID=$1
slot_power_control_lock="/run/slot_power_control_${CHASSIS_ID}.lock"
IO_EXP_SLOT_PWR_STATUS=$((CHASSIS_ID - 1))
IO_EXP_SLOT_PWR_CTRL=$((IO_EXP_SLOT_PWR_STATUS + 8))
#IO_EXP_BIC_PWR_CTRL=32
IO_EXP_SLOT_12V_STATUS=$((CHASSIS_ID + 23))

GPIOCHIP_IO_EXP_SLOT_PWR_CTRL=$(basename "/sys/bus/i2c/devices/$SPIDER_BOARD_IO_EXP_BUS_NUM-00$IO_EXP_SLOT_PWR_CTRL_ADDR/"*gpiochip*)
#GPIOCHIP_IO_EXP_BIC_PWR_CTRL=$(basename "/sys/bus/i2c/devices/$IO_EXP_SLOT_PWR_STATUS-00$IO_EXP_BIC_PWR_CTRL_ADDR/"*gpiochip*)
GPIOCHIP_IO_EXP_SLOT_12V_STATUS_CTRL=$(basename "/sys/bus/i2c/devices/$MANAGEMENT_BOARD_IO_EXP_BUS_NUM-00$IO_EXP_SLED_PWR_CTRL_ADDR/"*gpiochip*)

is_nuvoton_board="$(check_nuvoton_board)"

disable_i3c_hub()
{
    local target_slot="$CHASSIS_ID"
    local hub_path
    local offset_file
    local access_file
    local current_port
    local mask=1
    local i3c_hub_lock

    echo "Do 12V off disable i3c hub"
    if [ "$target_slot" -lt 5 ]; then
        hub_path="/sys/kernel/debug/i3c-hub-0-*/reg"
        i3c_hub_lock="/run/i3c-hub0.lock"
        echo "Slot$target_slot on i3c hub 0."
    else
        hub_path="/sys/kernel/debug/i3c-hub-1-*/reg"
        i3c_hub_lock="/run/i3c-hub1.lock"
        echo "Slot$target_slot on i3c hub 1."
    fi

    for file in $hub_path/{offset,access}; do
        if [[ $file == *"/offset" ]]; then
            offset_file="$file"
        elif [[ $file == *"/access" ]]; then
            access_file="$file"
        fi
    done

    {
        flock -x 100
        echo "Unlock i3c hub register."
        echo 16 > "$offset_file"
        echo 105 > "$access_file"

        echo "Disable slot${target_slot} i3c port."
        echo 18 > "$offset_file"
        current_port=$(cat "$access_file")
        if [ "$target_slot" -gt 4 ]; then
            ((target_slot=target_slot-4))
        fi
        mask=$((mask << (--target_slot)))
        mask=$((~mask))
        current_port=$((current_port & mask))
        echo $current_port > "$access_file"
        cat "$access_file"

        echo 81 > "$offset_file"
        echo $current_port > "$access_file"

        echo "Lock i3c hub register."
        echo 16 > "$offset_file"
        echo 0 > "$access_file"
    } 100>$i3c_hub_lock
}

# Server 12v power off
chassis-power-off()
{
        disable_i3c_hub
        if ! gpio_set "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_CTRL"=1
        then
                msg="Failed to set chassis$CHASSIS_ID power off"
                echo "${msg}"
                add_sel "${msg}"
        fi
        sleep 20

        # Check chassis status after doing 12V off
        chassis_status=$(gpio_get "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_STATUS")
        if [ "$chassis_status" == "$STATE_OFF" ]
        then
                busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_OFF_PROPERTY"
                echo "Chassis$CHASSIS_ID is power off"
        else
                busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_ON_PROPERTY"
                msg="Failed to power off Chassis$CHASSIS_ID, Chassis$CHASSIS_ID is power on"
                echo "${msg}"
                add_sel "${msg}"
                exit 0;
        fi
}

msg="Execute chassis$CHASSIS_ID power off"
echo "${msg}"
add_sel "${msg}"

exec 300>"$slot_power_control_lock"
if ! flock -n 300 ; then
    echo "Slot $CHASSIS_ID is currently running a power control operation, skipping."
    msg="Execute chassis$CHASSIS_ID power off fail, power control is operating"
    echo "${msg}"
    add_sel "${msg}"
    exit 1
fi

if ! chassis_status=$(gpio_get "$GPIOCHIP_IO_EXP_SLOT_PWR_CTRL" "$IO_EXP_SLOT_PWR_STATUS"); then
    echo "Failed to get chassis status"
    exit 1
fi

if [ "$chassis_status" == "$STATE_ON" ]
then
        if [ -n "$is_nuvoton_board" ]
        then
                # inform management CPLD that the slot is 12V off
                if ! gpio_set "$GPIOCHIP_IO_EXP_SLOT_12V_STATUS_CTRL" "$IO_EXP_SLOT_12V_STATUS"=0
                then
                        echo "Failed to inform management CPLD that chassis$1 is 12V off"
                fi
        fi

        # Check if PRSNT_SB_SLOT{1~8}_N is exist, if not exist, continues power-off process
        if [ ! -f "/tmp/gpio/PRSNT_SB_SLOT${1}_N" ]; then
            echo "PRSNT_SB_SLOT${1}_N is not exist, continue power-off process"
        else
            slot_presence=$(cat /tmp/gpio/PRSNT_SB_SLOT"${1}"_N)
            if [ "$slot_presence" -ne 0 ]; then
                echo "Chassis$1 is not present, skip power-off process"
                exit 1
            fi
        fi

        chassis-power-off
        exit 0;
else
        echo "Chassis$CHASSIS_ID is already off"
        exit 0;
fi
